package com.xhtech.base.ext.databinding

import android.text.TextUtils
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.FloatRange
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.xhtech.base.R
import com.yxh.lib_base.ext.dp2px
import com.yxh.lib_base.util.image.ImageLoadUtils
import com.yxh.lib_base.util.image.load

/**
 *@创建者：XH-hua
 *@创建时间：2021/5/8 17:16
 *@描述：自定义bindingAdapter-图片相关
 */
object CustomBindAdapterImage {

//    @JvmStatic
//    @BindingAdapter(value = ["imgUrl"], requireAll = true)
//    fun setImgUrl(imageView: ImageView, url: String?) {
//        checkImgUrlEmpty(imageView, url) {
//            setImgUrl(imageView, url, R.mipmap.image_placeholder_fail)
//        }
//    }
//
    @JvmStatic
    @BindingAdapter(value = ["imgUrl", "imgError"], requireAll = true)
    fun setImgUrl(imageView: ImageView, url: String?, @DrawableRes imgError: Int) {
        if (url == null) {
            imageView.load(imgError)
            return
        }
//        var loadUrl = NetBaseApi.checkImageUrlAppendBaseUrlPrefix(url)
        Glide.with(imageView.context)
                .load(url)
                .error(ResourceUtils.getDrawable(imgError))
                .transition(DrawableTransitionOptions.withCrossFade(400))
                .into(imageView)
    }

    @JvmStatic
    @BindingAdapter(value = ["imgUrl", "radius"], requireAll = true)
    fun setImgUrl(imageView: ImageView, imgUrl: String?, radius: Float? = 0f) {
        checkImgUrlEmpty(imageView, imgUrl) {
            if (radius != null && radius != 0f) {
                ImageLoadUtils.getEngine().load(imgUrl,imageView)
            } else {
                setImgUrl(imageView, imgUrl, R.drawable.app_ic_avatar)
            }
        }
    }

    private fun checkImgUrlEmpty(imageView: ImageView, imgUrl: String?, factory: () -> Unit) {
        if (!TextUtils.isEmpty(imgUrl)) {
            factory.invoke()
        } else {
            setImgSrc(imageView, R.drawable.app_ic_avatar)
        }
    }


//    /**
//     * @作用：灰度图
//     * @param url 网络图片地址
//     * @param proportion 进度
//     * */
//    @JvmStatic
//    @BindingAdapter(value = ["imgUrlGrayscale", "proportion"], requireAll = true)
//    fun setImgGrayscale(
//            imageView: ImageView,
//            imgUrlGrayscale: String,
//            @FloatRange(from = 0.0, to = 1.0) proportion: Float? = 0f
//    ) {
//        ImageLoaderMgr.instance().loadBadgeGreyPic(imageView, imgUrlGrayscale, proportion ?: 0f)
//    }
//
//

    @BindingAdapter(value = ["loadUserHead"])
    @JvmStatic
    fun loadUserHead(view: ImageView, url: String) {
        view.load(url){
            error(R.drawable.app_ic_avatar)
        }
    }


    /**
     * 由宽高值算Imageview实际高度
     */
    @JvmStatic
    @BindingAdapter(value = ["widthHeight"], requireAll = false)
    fun resize(imageView: ImageView, widthHeight: Array<Int>?) {
        var realWidth = (ScreenUtils.getScreenWidth() - 32.0f.dp2px()) / 2
        var layoutParams: ViewGroup.LayoutParams = imageView.layoutParams
        layoutParams.width = realWidth
        layoutParams.height = (realWidth.toFloat() / (widthHeight?.get(0)
                ?: 100) * (widthHeight?.get(1) ?: 100)).toInt()
        imageView.layoutParams = layoutParams
    }


    @BindingAdapter("android:src")
    fun setImageViewResource(imageView: ImageView, resource: Int) {
        imageView.setImageResource(resource)
    }

    @JvmStatic
    @BindingAdapter(value = ["imgSrc"], requireAll = true)
    fun setImgSrc(imageView: ImageView, @DrawableRes drawableRes: Int) {
        imageView.setImageDrawable(ResourceUtils.getDrawable(drawableRes))
    }

//
//    @JvmStatic
//    @BindingAdapter(value = ["initSVGAImageView"], requireAll = true)
//    fun setSVGAImageView(svgaImageView: SVGAImageView, boolean: Boolean) {
//        if (boolean) {
//            svgaImageView.show()
//            svgaImageView.startAnimation()
//        } else {
//            svgaImageView.stopAnimation(true)
//            svgaImageView.gone()
//        }
//    }
//
//    @JvmStatic
//    @BindingAdapter(value = ["showBadgeLight"], requireAll = true)
//    fun showBadgeLight(svgaImageView: SVGAImageView, boolean: Boolean) {
//        if (boolean) {
//            svgaImageView.show()
//            SVGAPlayerHelper.loadSAssetsVGAAnimation(
//                    svgaImageView,
//                    SVGAPlayerHelper.Path.BadgeLight
//            )
//        } else {
//            svgaImageView.stopAnimation(true)
//            svgaImageView.gone()
//        }
//    }
//
//
//    @JvmStatic
//    @BindingAdapter(value = ["showXingXingLight"], requireAll = true)
//    fun showXingXingLight(svgaImageView: SVGAImageView, stage: Int) {
//        if (stage > 0) {
//            svgaImageView.show()
//            val svgPath = when (stage) {
//                1 -> SVGAPlayerHelper.Path.ExpressLike
//                2 -> SVGAPlayerHelper.Path.Rose_2
//                else -> SVGAPlayerHelper.Path.stars_shine
//            }
//            SVGAPlayerHelper.loadSAssetsVGAAnimation(svgaImageView, svgPath)
//        } else {
//            svgaImageView.stopAnimation(true)
//            svgaImageView.gone()
//        }
//    }


}